package view;

import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;

import utils.Point3D;

import mathematics.Matrix4x4;
import model.AdjacencyModel;
import model.RoomModel;

import graphics.engine.*;

public class AdjacencyView  extends BaseView
{
	public AdjacencyModel model;
	
	public RoomView firstRoom;
	public RoomView secondRoom;
	
	public AdjacencyView(AdjacencyModel model, RoomView firstRoom, RoomView secondRoom)
	{
		super(model);
		this.model = model;
		this.firstRoom = firstRoom;
		this.secondRoom = secondRoom;
		//this.link = new Polyline(gl, points, lineWidth, false);
	}
	
	public void Draw(GLAutoDrawable drawable, ProgramShader programShader) 
	{
		boolean mustDrawLink = false;
		GL gl = drawable.getGL();
		Point3D beginPoint;
		Point3D endPoint;
		
		if(this.firstRoom.isNodeShape())
		{
			beginPoint = ((RoomModel)this.firstRoom.getModel()).computedPosition();
			mustDrawLink = true;
		}
		else
		{
			beginPoint = this.model.AnchoragePoint;
		}
		if(this.secondRoom.isNodeShape())
		{
			endPoint =  ((RoomModel)this.secondRoom.getModel()).computedPosition();
			mustDrawLink = true;
		}
		else
		{
			endPoint = this.model.AnchoragePoint;
		}
		
		
		if(mustDrawLink)
		{
			gl.glUniformMatrix4fv(programShader.WorldMatrixUniformID, 1, false, this.WorldMatrix(), 0);		

			gl.glLineStipple(1, (short) 0x0F0F);  
			gl.glEnable(GL.GL_LINE_STIPPLE);		
			
			gl.glLineWidth(1.0f);			
			gl.glBegin(GL.GL_LINES);

	
			gl.glColor3f(0, 0, 0);
			gl.glVertex3f((float)beginPoint.x(), (float)beginPoint.y(),  5.0f);
			gl.glVertex3f((float)endPoint.x(), (float)endPoint.y(),  5.0f);
			
			gl.glEnd();

			gl.glDisable(GL.GL_LINE_STIPPLE);
		}	
	}
	
	public float[] WorldMatrix()
	{
		Matrix4x4 worldMatrix = new Matrix4x4();
		
		worldMatrix.setToIdentity();
		
		return worldMatrix.toFloatArray();
	}

	@Override
	public void update() {
		// TODO Auto-generated method stub
		
	}
}
